package cn.kgc.springcloud.controller;

import com.netflix.loadbalancer.IRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: mosin
 * @version: v1.0  2023/1/3
 */
@Controller
@RequestMapping("exam")
public class ExamController {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;


    @RequestMapping("m1")
    @ResponseBody
    public String method1(){
        // 发送http请求  调用score服务的接口 discoveryClient
        RestTemplate restTemplate = new RestTemplate();
        List<ServiceInstance> scoreclient = discoveryClient.getInstances("SCORECLIENT");
        for (ServiceInstance serviceInstance : scoreclient) {
            System.out.println("host="+serviceInstance.getHost());
            System.out.println("port="+serviceInstance.getPort());
            System.out.println("uri="+serviceInstance.getUri());
        }
        Random random = new Random();
        int i = random.nextInt(scoreclient.size());
        ServiceInstance serviceInstance = scoreclient.get(i);
        URI uri = serviceInstance.getUri();
        String info = restTemplate.getForObject(uri + "/score/m1", String.class);
        return "ExamService ok : " +info;
    }

    @RequestMapping("m2")
    @ResponseBody
    public String method2(){
        // 发送http请求  调用score服务的接口
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject("http://"+randomHost()+"/score/m1", String.class);
        System.out.println("info = " + info);
        return "ExamService ok : "+info;
    }


    @RequestMapping("m3")
    @ResponseBody
    public String method3(){
        // 发送http请求  调用score服务的接口  默认的负载均衡机制  轮循
        RestTemplate restTemplate = new RestTemplate();
        ServiceInstance scoreclient = loadBalancerClient.choose("SCORECLIENT");
        URI uri = scoreclient.getUri();
        String info = restTemplate.getForObject(uri + "/score/m1", String.class);
        return "ExamService ok : "+ info;
    }


    @RequestMapping("m4")
    @ResponseBody
    public String method4(){

        // 发送http请求  调用score服务的接口
        String info = restTemplate.getForObject("http://SCORECLIENT/score/m1", String.class);
        return "ExamService ok : "+ info;
    }

    public String randomHost(){

        ArrayList<String> hosts = new ArrayList<>();
        hosts.add("localhost:8902");
        hosts.add("localhost:8903");

        Random random = new Random();
        int index = random.nextInt(hosts.size());

        return hosts.get(index);
    }


}
